package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResChoiceRechargeEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductRebateEntity;
import com.taomu.app.zongchouhuli.http.res.ResRebateDeatailEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserRebateDetailContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateDetailEntity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UserRebateDetailPresenter implements UserRebateDetailContract.Presenter {

    private UserRebateDetailContract.View mContractView;

    private CompositeDisposable disposable;

    public UserRebateDetailPresenter(UserRebateDetailContract.View mConatractView){
        this.mContractView = mConatractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getRebateDetailList(int ship,int take,String id) {
        Request.getIntence().getProductIncomeDetail(String.valueOf(ship),String.valueOf(take),id)
                .flatMap(new Function<String, ObservableSource<BaseChangeResponse<ResProductRebateEntity>>>() {
                    @Override
                    public ObservableSource<BaseChangeResponse<ResProductRebateEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResProductRebateEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResProductRebateEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResProductRebateEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResProductRebateEntity> resProductEntities) {
                        if(mContractView != null){
                            mContractView.setRebateDetailList(resProductEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });
    }

    @Override
    public void sellProduct(String id) {
        Request.getIntence().getSellProduct(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String resProductEntities) {
                        if(mContractView != null){
                            mContractView.setSellResults();
                        }
                    }
                });
    }

    @Override
    public void getRebateDetail(String id) {
        Request.getIntence().getRebateDetailInfo(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResRebateDeatailEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResRebateDeatailEntity resProductEntities) {
                        if(mContractView != null){
                            mContractView.setRebateDetail(resProductEntities);
                        }
                    }
                });
    }

    @Override
    public void changedJiesun(String id, String income_type) {
        Request.getIntence().getChangeJiesuan(id, income_type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String msg) {
                        if(mContractView != null){
                            mContractView.setChanged(msg);
                        }
                    }
                });
    }

    @Override
    public void detch() {

    }

    public void setRebateDetailList(){
        List<RebateDetailEntity> mData = new ArrayList<>();
        for(int i=0;i<10;i++){
            RebateDetailEntity entity = new RebateDetailEntity();
            entity.setTitle("结算");
            entity.setPrice("+100");
            entity.setTime("2018-08-30 14:36");
            mData.add(entity);
        }
        if(mContractView != null){
           // mContractView.setRebateDetailList(mData);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
